#include <iostream>
#include <queue>
#include <vector>
#include <cstring>

using namespace std;

const int max_n = 1e4 + 10;
const int inf = 0x3f3f3f3f;

struct Node {
    int to, w;
} tmp;

vector<Node> w[max_n];
int d[max_n], vis[max_n];

void spfa(int c) {
    memset(d, 0x3f, sizeof(d));
    d[c] = 0;
    queue<int> q;
    q.push(c);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        vis[x] = 1;
        for (int i = 0; i < w[x].size(); i++) {
            int v = w[x][i].to;
            if (d[x] + w[x][i].w < d[v]) {
                d[v] = d[x] + w[x][i].w;
                if (!vis[v]) {
                    q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }

    return ;
}

int main() {
    int n, m, c;
    scanf("%d%d%d", &n, &m, &c);
    for (int i = 0; i < m; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        tmp.to = x, tmp.w = 1;
        w[x].push_back(tmp);
        tmp.to = y;
        w[y].push_back(tmp);
    }
    spfa(c);
    for (int i = 0; i < n; i++) {
        printf("%d\n", d[i]);
    }

    return 0;
}

